﻿namespace AntdUI_HamburgerMenuTabs.Pages.Demo
{
    public partial class Select : UserControl
    {
        public Select()
        {
            InitializeComponent();

            InitSelect1();
        }

        private void InitSelect1()
        {
            select1.AllowClear = true;
            select1.ListAutoWidth = true;
            select1.MaxCount = 8;

            select1.TextChanged += (s, e) =>
            {
                AddLog($"[Select1.TextChanged]\r\nText: {select1.Text}");
            };

            select1.SelectedValueChanged += (s, e) =>
            {
                AddLog($"[Select1.SelectedValueChanged] [无论是否选择同一项都会触发本事件]\r\nValue: {e.Value}");
            };

            select1.SelectedIndexChanged += (s, e) =>
            {
                if (select1.SelectedIndex >= 0)
                {
                    var item = select1.Items[e.Value];
                    if (item is AntdUI.SelectItem _item)
                    {
                        AddLog($"[Select1.SelectedIndexChanged] [无论是否选择同一项都会触发本事件 / 点击 Sub 时无法正确获取 Item]\r\nSelectedIndex: {e.Value}\r\nText: {_item.Text}\r\nTag: {_item.Tag}\r\nSubText: {_item.SubText}\r\nSelect1.Text: {select1.Text}\r\nSelect1.SelectedValue: {select1.SelectedValue}");
                    }
                }
            };

            List<object> items1 = [];
            for (int i = 0; i < 10; i++)
            {
                var item = new AntdUI.SelectItem($"选项 A{(i + 1):000}", $"{Guid.NewGuid().ToString().ToUpper().Replace("-", "")}") { SubText = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffffff}" };
                if (i == 0)
                {
                    item.Sub = [];
                    for (int j = 0; j < 5; j++)
                    {
                        item.Sub.Add(new AntdUI.SelectItem($"选项 A{(i + 1):000}.{(j + 1):000}", $"{Guid.NewGuid().ToString().ToUpper().Replace("-", "")}") { SubText = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffffff}" });
                    }
                }
                items1.Add(item);
            }
            items1.Add(new AntdUI.DividerSelectItem());
            for (int i = 0; i < 10; i++)
            {
                var item = new AntdUI.SelectItem($"选项 B{(i + 1):000}", $"{Guid.NewGuid().ToString().ToUpper().Replace("-", "")}") { SubText = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffffff}" };
                items1.Add(item);
            }
            select1.Items.AddRange(items1);
        }

        private void AddLog(string log)
        {
            if (inputLog.Text.Length > 0) inputLog.Text += "\r\n\r\n";
            inputLog.Text += $"[{DateTime.Now:HH:mm:ss.ffffff}] {log}";
            inputLog.ScrollToCaret();
            inputLog.SelectionStart = inputLog.Text.Length;
        }
    }
}